package com.example.treasure_back.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.treasure_back.mapper.QuestionMapper;
import com.example.treasure_back.model.QuestionModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@Service
public class QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Resource
    @Qualifier("myRedisTemplate")
    private RedisTemplate<String, Integer> redisTemplate;

    public Integer getRandomQuestion(){
        QueryWrapper<QuestionModel> queryWrapper = new QueryWrapper<>();
        List<QuestionModel> questionModels = questionMapper.selectList(queryWrapper);
        int sum = questionModels.size();
        Random random = new Random();
        return random.nextInt(sum) + 1;
    }

    public QuestionModel getQuestion(Integer id){
        return questionMapper.selectById(id);
    }

    public boolean isRight(Integer id, String answer){
        QuestionModel questionModel = questionMapper.selectById(id);
        return questionModel.getAnswer().equals(answer);
    }
}
